package com.chansos.dubbogo.project_generator;

import com.chansos.dubbogo.util.TemplateUtils;
import com.intellij.openapi.project.Project;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SampleCodeGenerator implements ProjectGenerator {

    private final List<TemplateFileModel> templateFiles = new ArrayList<>() {{
        String basePath = "/project_template/default_sample";

        // Go Module
        add(new TemplateFileModel(basePath, "/mod", "/go.mod"));

        // Dubbo Go Api Pkg
        add(new TemplateFileModel(basePath, "/api_file", "/api/samples_api.pb.go"));
        add(new TemplateFileModel(basePath, "/api_triple", "/api/samples_api_triple.pb.go"));
        add(new TemplateFileModel(basePath, "/api_proto", "/api/samples_api.proto"));

        // Dubbo Go Client
        add(new TemplateFileModel(basePath, "/client_config", "/go-client/conf/dubbogo.yaml"));
        add(new TemplateFileModel(basePath, "/client", "/go-client/cmd/client.go"));

        // Dubbo Go Server
        add(new TemplateFileModel(basePath, "/server_config", "/go-server/conf/dubbogo.yaml"));
        add(new TemplateFileModel(basePath, "/server", "/go-server/cmd/server.go"));

        // Project Run Configuration
        add(new TemplateFileModel(basePath, "/.run/client.run.xml", "/.run/client.run.xml"));
        add(new TemplateFileModel(basePath, "/.run/server.run.xml", "/.run/server.run.xml"));
    }};

    @Override
    public void generate(Project projectName, String path, Map<String, String> args) {
        for (TemplateFileModel templateFile : templateFiles) {
            String content = TemplateUtils.readTemplate(templateFile.getTemplatePath());
            if (content != null) {
                content = TemplateUtils.replaceContent(content, args);
                templateFile.setModulePath(path);
                TemplateUtils.writeTemplate(templateFile.getTargetPath(), content);
            }
        }
    }

}
